package com.p2p.quartz;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.account.service.GroupsService;
import com.p2p.common.utils.DateUtil;
import com.p2p.daily.po.P2pDailyOutput;
import com.p2p.daily.service.P2pDailyOutputService;
import com.p2p.wallet.po.Wallet;
import com.p2p.wallet.service.WalletService;

public class DataOutputJob {


	@Resource
	private RedisTemplate redisTemplate;

	@Autowired
	private WalletService walletService;

	@Autowired
	private P2pDailyOutputService p2pDailyOutputService;

	@Autowired
	private GroupsService groupsService;

	// 启动任务
	public void start() {


		String terminalKey = "confirm-output:"
				+ DateUtil.date2Str(new Date(System.currentTimeMillis() - 3600 * 1000 * 2), DateUtil.FORMAT_DATE_TIME3);
		String dateTime = DateUtil.date2Str(new Date(System.currentTimeMillis() - 3600 * 1000 * 2),
				DateUtil.FORMAT_DATE_TIME3);
		
		String dateTime2 = DateUtil.date2Str(new Date(System.currentTimeMillis() - 3600 * 1000 * 2),
				DateUtil.FORMAT_DATE2);
		if (redisTemplate.hasKey(terminalKey)) {
			Map<String, String> entries = redisTemplate.opsForHash().entries(terminalKey);
			Set<Entry<String, String>> entrySet = entries.entrySet();
			for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) {
				Entry<String, String> entry = (Entry<String, String>) iterator.next();
				String key = entry.getKey();
				String value = entry.getValue();
				String[] values = value.split(",");
				Wallet wallet = walletService.selectOne(new EntityWrapper<Wallet>().eq("status", 1).eq("coin_id", key));
				if (wallet != null) {
					String coinId = wallet.getCoinId();
					P2pDailyOutput p2pDailyOutput = p2pDailyOutputService.selectOne(new EntityWrapper<P2pDailyOutput>()
							.eq("coin_type", coinId).eq("date_time", dateTime2));
					
					

					String poolKey = key + ":" + dateTime;
					
					Object priceStr = redisTemplate.opsForHash().get(poolKey, "price");
					Object countStr = redisTemplate.opsForHash().get(poolKey, "count");
					Object currentHashrateStr = redisTemplate.opsForHash().get(poolKey, "currentHashrate");
					if(priceStr==null){
						priceStr="0";
					}
					if(countStr==null){
						countStr="0";
					}
					if(currentHashrateStr==null){
						currentHashrateStr="0";
					}
					
				
					BigDecimal price=	new BigDecimal(priceStr.toString());
					BigDecimal count=	new BigDecimal(countStr.toString());
					Double currentHashrate=	new Double(currentHashrateStr.toString());
					
					if (p2pDailyOutput == null) {
						p2pDailyOutput = new P2pDailyOutput();
						
						p2pDailyOutput.setDateTime(DateUtil.str2Date(dateTime2, DateUtil.FORMAT_DATE2));
						p2pDailyOutput.setWalletId(wallet.getId());
						p2pDailyOutput.setWalletName(wallet.getName());
						p2pDailyOutput.setWalletAddress(wallet.getWalletAddress());
						
						p2pDailyOutput.setUpdateTime(new Date());
						p2pDailyOutput.setCoinType(key);
						p2pDailyOutput.setRecentSubmitTime(new Date());
						p2pDailyOutput.setAgentCount(Integer.parseInt(values[0]));
						
						p2pDailyOutput.setTerminalCount(Integer.parseInt(values[2]));
						p2pDailyOutput.setGroupsName("");
						p2pDailyOutput.setPoolAddress(wallet.getPoolMark());
						p2pDailyOutput.setDayPrice(price);
						
						p2pDailyOutput.setCoinCount(count);
						p2pDailyOutput.setCurrentHashrate(currentHashrate);
						p2pDailyOutput.setPredictIncome(count.multiply(price));
						p2pDailyOutput.setStatus(0);
					}else{
						Integer status = p2pDailyOutput.getStatus();
						if(status==1){
							continue;
						}
						p2pDailyOutput.setUpdateTime(new Date());
						p2pDailyOutput.setAgentCount(Integer.parseInt(values[0]));
						p2pDailyOutput.setTerminalCount(Integer.parseInt(values[2]));
						
						if(!price.equals(new BigDecimal(0))&&!count.equals(new BigDecimal(0))&&!currentHashrate.equals(0.0d)){
							p2pDailyOutput.setDayPrice(price);
							p2pDailyOutput.setCoinCount(count);
							p2pDailyOutput.setCurrentHashrate(currentHashrate);
							p2pDailyOutput.setPredictIncome(count.multiply(price));
						}												
					}
					p2pDailyOutputService.insertOrUpdate(p2pDailyOutput);
				}
			}
		}

	}

}
